package person.aoao.library.controller;

import person.aoao.library.entity.RoleList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.RoleListService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (RoleList)表控制层
 *
 * @author makejava
 * @since 2023-05-07 23:00:09
 */
@RestController
@CrossOrigin
@RequestMapping("roleList")
public class RoleListController {
    /**
     * 服务对象
     */
    @Resource
    private RoleListService roleListService;

    /**
     * 分页查询
     *
     * @param roleList    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<RoleList>> queryByPage(RoleList roleList, PageRequest pageRequest) {
        return ResponseEntity.ok(this.roleListService.queryByPage(roleList, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<RoleList> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.roleListService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param roleList 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<RoleList> add(RoleList roleList) {
        return ResponseEntity.ok(this.roleListService.insert(roleList));
    }

    /**
     * 编辑数据
     *
     * @param roleList 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<RoleList> edit(RoleList roleList) {
        return ResponseEntity.ok(this.roleListService.update(roleList));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.roleListService.deleteById(id));
    }


    @GetMapping("get")
    public ResponseData getRoleList(){
        List<RoleList> list=this.roleListService.getRoleList();
        return new ResponseData(ResponseCode.SUCCESS,list);
    }

    @PostMapping("addRole")
    public ResponseData addRole(@RequestBody RoleList roleList){
        RoleList item=this.roleListService.insert(roleList);
        return new ResponseData(ResponseCode.SUCCESS,item.getRoleid());
    }

    @PostMapping("editRole")
    public ResponseData editRole(@RequestBody RoleList roleList){
        this.roleListService.update(roleList);
        return new ResponseData(ResponseCode.SUCCESS);
    }

    @GetMapping("deleteRole/{roleid}")
    public ResponseData deleteRole(@PathVariable Integer roleid){
        Boolean bool=this.roleListService.deleteById(roleid);
        if(bool){
            return  new ResponseData(ResponseCode.SUCCESS);
        }else{
            return  new ResponseData(ResponseCode.Acount_Yes);
        }

    }

    @GetMapping("getRoleInfo/{roleid}")
    public ResponseData getRoleInfo(@PathVariable Integer roleid){
        RoleList info=this.roleListService.queryById(roleid);
        return  new ResponseData(ResponseCode.SUCCESS,info);
    }

    @PostMapping("searchRole")
    public ResponseData searchRole(@RequestBody String value){
        System.out.println(value.replace("\"", ""));
        List<RoleList> list=this.roleListService.searchRole(value.replace("\"", ""));
        return new ResponseData(ResponseCode.SUCCESS,list);
    }
}

